<?php

namespace app\index\controller;

use think\Controller;
use think\Cookie;

class Cash extends Controller
{
    public static $userid;
    public function _initialize()
    {
        if (!Cookie::has('uid')) {
            $this->redirect('user/index');
        }
        self::$userid = cookie('uid');
//        $this->assign('sysConfig', $sysConfig);
    }

    /**
     * 提现
     */
    public function cash(){
       $userinfo = db('user')->where('id',self::$userid)->find();
        if(request()->isPost()){
            $data = input('post.');
            if(!abs((int)$data['money']*100)){
                $this->result('',0,'提现金额不合法');
            }
            if($userinfo['balance']<$data['money']){
                $this->result('',0,'账户余额不足');
            }
            if(!$this->check_times()){
                $this->result('',0,'超过今日提现次数');
            }
            $data['uid'] = self::$userid;
            $data['create_time'] = time();
           $dec = db('user')->where('id',self::$userid)->setDec('balance',$data['money']);
           if($dec){
               $res = db('cash')->insert($data);
               if($res){
                   return $this->result('',1,'提交申请成功');
               }else{
                   return $this->result('',0,'申请失败');
               }
           }
        }
    }

    /*
     * 检测今日提现次数
     */
    public function check_times(){
        $begin = strtotime(date('Y-m-d') . '00:00');
        $where = [
          'uid' => self::$userid,
           'create_time' => [['ELT',$begin],['EGT',time()]]
        ];
       $num = db('cash')->where($where)->count();
       if($num>5){
           return false;
       }else{
           return true;
       }
    }

}
